//
//  NKCloudChoseView.m
//  NewKY
//
//  Created by MXQ on 2016/11/14.
//  Copyright © 2016年 Dee. All rights reserved.
//

#import "NKCloudChoseView.h"

@interface NKCloudChoseView ()<UITableViewDataSource,UITableViewDelegate>


@end

@implementation NKCloudChoseView

- (instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
        
        [self setupCloudChoseView];
        
    }

    return self;


}

//setUp 标签选择的TableView

- (void)setupCloudChoseView{
    
    //实例化储存数组

    self.cloudesChosed = [[NSMutableArray alloc]init];

    //准备一个数组作为数据源
    _cloudList = [NSArray arrayWithObjects:@"腹痛",@"厌食",@"乏力",@"恶心",@"体重减轻",@"呕吐",@"吐血",nil];


    UITableView * tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    
    
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Kaka"] ;
    
    [self addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.equalTo(self);
        make.bottom.equalTo(self);
        
    }];
    
    self.tableView = tableView;
    
    
    tableView.dataSource =self;
    tableView.delegate = self;
    
    
    
    
}
// 实现数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableViewP{
    
    return  1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _cloudList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Kaka" forIndexPath:indexPath];
    
    
    cell.textLabel.text = _cloudList[indexPath.row];

    UIButton * button = [[UIButton alloc]init];
    
    [button setImage:[UIImage imageNamed:@" "] forState:UIControlStateNormal];
    
    [button setImage:[UIImage imageNamed:@"point"] forState:UIControlStateSelected];
//
    cell.accessoryView = button;
//
//    [button mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.height.with.equalTo(@12);
//        make.right.equalTo(cell.contentView);
//        
//    }];
////
//
//    
//    [button sizeToFit];
//    
    

    
    
    
    return cell;
    
}
//tableview代理方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath ];

    UIButton * btn = (UIButton *)cell.accessoryView;

    btn.selected = !btn.selected;
    
//    for (NSString * item  in self.cloudesChosed) {
//        
//        if ([self.cloudList[indexPath.row] isEqualToString:item] ) {
//
//            [self.cloudesChosed removeObject:item];
//            
//        }
//    }
//    

    [self.cloudesChosed enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([self.cloudList[indexPath.row] isEqualToString:obj] ) {
            
            *stop = YES;
            
            if (*stop == YES) {
                
                [self.cloudesChosed removeObject:obj];
                
            }
        }
    }];
    
    
    if (btn.selected) {
    
        [self.cloudesChosed addObject:self.cloudList[indexPath.row]];
        
    }

    NSLog(@"%@",self.cloudesChosed);
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
